package com.ayuku.retail.ui.category;

import org.eclipse.swt.SWT; 
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Shell;

import com.ayuku.commons.FormDialogBasic;
import com.ayuku.commons.Util;
import com.ayuku.commons.UtilPojo;
import com.ayuku.retail.business.category.CategoryImpl;
import com.ayuku.retail.persistence.model.Category;

/**
 * This code was edited or generated using CloudGarden's Jigloo SWT/Swing GUI
 * Builder, which is free for non-commercial use. If Jigloo is being used
 * commercially (ie, by a corporation, company or business for any purpose
 * whatever) then you should purchase a license for each developer using Jigloo.
 * Please visit www.cloudgarden.com for details. Use of Jigloo implies
 * acceptance of these licensing terms. A COMMERCIAL LICENSE HAS NOT BEEN
 * PURCHASED FOR THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED LEGALLY FOR
 * ANY CORPORATE OR COMMERCIAL PURPOSE.
 */
public class FormCategory extends FormDialogBasic {
	private CompCategory cmp;
	private String code;
	private String name;
	private Category pjo;

	public FormCategory(final Shell parent, final int style, final int mode) {
		super(parent, style, mode);
	}

	public void getPanelContent(final Group grpData) {
		cmp = new CompCategory(grpData, SWT.NONE);
	}

	@Override
	public void additionalProses() {
		getDialogShell().setText("Add/Edit Category");
		getDialogShell().setSize(350, 160);
		Util.setCenter(getDialogShell());
	}

	@Override
	public void clearContols() {
		cmp.txtCode.setText("");
		cmp.txtName.setText("");
	}

	@Override
	protected void doLoadAdd(String key) {
		// TODO Auto-generated method stub

	}

	@Override
	public void doLoad(String key) throws Exception {
		pjo = CategoryImpl.getById(key);
		cmp.txtCode.setText(Util.nullToString(pjo.getCode()));
		cmp.txtName.setText(Util.nullToString(pjo.getName()));
	}

	@Override
	public boolean doValidate() {
		code = cmp.txtCode.getText();
		if (Util.isEmpty(code)) {
			Util.showMsgBox("code harus dilengkapi!", 1,
					getDialogShell());
			cmp.txtCode.setFocus();
			return false;
		}

		name = cmp.txtName.getText();
		if (Util.isEmpty(name)) {
			Util.showMsgBox("nama harus dilengkapi!", 1, getDialogShell());
			cmp.txtName.setFocus();
			return false;
		}

		return true;
	}

	@Override
	public void enabledControl(final boolean otherField, final boolean keyField) {
		cmp.txtCode.setEditable(keyField);
		cmp.txtName.setEditable(otherField);
	}

	@Override
	public int doMainInsert() throws Exception {
		pjo = new Category();
		setPojoWithData();
		return CategoryImpl.add(pjo);
	}

	@Override
	public int doMainDelete() throws Exception {
		return CategoryImpl.delete(pjo.getCode()+"");
	}

	@Override
	public int doMainUpdate() throws Exception {
		setPojoWithData();
		return CategoryImpl.update(pjo);

	}

	@Override
	protected void setPojoWithData() throws Exception {
		pjo.setCode(code);
		pjo.setName(name);
		UtilPojo.setDateAndUser(pjo);
	}

	@Override
	protected void disabledOtherButton(boolean enable) {
		// TODO Auto-generated method stub

	}

}
